package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.UserMapper;
import com.zhongyu.pojo.User;
import com.zhongyu.service.UserService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

  /*  @Resource
    private AutoMapper autoMapper;*/
    @Resource
private UserMapper userMapper;
    @Override
    public List<User> searchByUserVo(UserVo userVo) {
      /*  QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),"user_code",userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,"user_role",userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,"birthday",userVo.getStartDate())
                .le(userVo.getEndDate()!=null,"birthday",userVo.getEndDate());
          return  super.list(queryWrapper);*/
        LambdaQueryWrapper<User> lambdaQueryWrapper= Wrappers.<User> lambdaQuery();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
             return super.list(lambdaQueryWrapper);

    }
    @Override
    public void updatePwd(String password, String userCode) {

         userMapper.updatePwd(password,userCode);
    }
   /* @Override
    public PageResult searchbyUserVo(Integer pageNo, Integer pageSize, UserVo userVo) {
       // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
        LambdaQueryWrapper<User> queryWrapper=Wrappers.<User>lambdaQuery();
        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
        Page page=new Page(pageNo,pageSize);
        IPage<User> iPage= super.page(page,queryWrapper);
        return PageResult.getInstance(iPage);

    }
*/
 /* @Override
    public PageResult searchbyUserVo(Integer pageNo, Integer pageSize, UserVo userVo) {
       // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
        LambdaQueryWrapper<User> queryWrapper=Wrappers.<User>lambdaQuery();
        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
        Page page=new Page(pageNo,pageSize);
        IPage<User> iPage= super.page(page,queryWrapper);

        PageResult pageResult= PageResult.getInstance(iPage);
        pageResult.getRows().forEach(autoMapper::mapperEntity);
        return pageResult;
    }*/
     /*public List<User> searchAll( UserVo userVo) {
         // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
         LambdaQueryWrapper<User> queryWrapper=Wrappers.<User>lambdaQuery();
         queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                 .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                 .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                 .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
         return userMapper.findAll(queryWrapper);
     }*/

    public  PageResult searchAll(Integer pageNo, Integer pageSize, UserVo userVo) {
        // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
        LambdaQueryWrapper<User> wrapper=Wrappers.<User>lambdaQuery();
        wrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                .like(!StringUtils.isEmpty(userVo.getUserName()),User::getUsername,userVo.getUserName())
                .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                .eq(userVo.getGender()!=null,User::getGender,userVo.getGender())
                .eq(userVo.getUserOrg()!=null,User::getUserOrg,userVo.getUserOrg())
                .like(!StringUtils.isEmpty(userVo.getPhone()),User::getPhone,userVo.getPhone())
                .eq(!StringUtils.isEmpty(userVo.getAddress()),User::getAddress,userVo.getAddress())
                .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
        Page page=new Page(pageNo,pageSize);
        List<User> list=userMapper.findAll(page,wrapper);
//       System.out.println("==2>"+list.size());
       /* return PageResult.getInstanceByPage(page);*/
      return new PageResult(list,page.getTotal());
    }

    @Override
    public List<User> searchAll() {
        return userMapper.selectList(null);
    }

    @Override
    public User searchByUserCode(String userCode) {
        LambdaQueryWrapper<User> wrapper=Wrappers.<User> lambdaQuery();
        wrapper.eq(User::getUserCode,userCode);
        return super.getOne(wrapper);

    }

    @Override
    public void update(Integer userId, Integer userRole) {
        userMapper.update(userId,userRole);
    }

    @Override
    public void up(String newPas, String username) {
        userMapper.up(newPas,username);
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void del(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public String selectUserCode(String userCode) {
        return userMapper.selectUserCode(userCode);
    }

    @Override
    public String selectPassword(String password,String username) {
        return userMapper.selectPassword(password,username);
    }


}
